package vista;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import client.FtpClient;

import comum.InfoArquivo;
import comum.ListaArquivo;

import controle.RequestManager;

public class TelaCliente extends JFrame {

	private static final long serialVersionUID = 1L;
	private static final TelaCliente INSTANCE = new TelaCliente();

	int qtPastasUser,qtArqUser;
	
	public int qtPastasServidor, qtArqServidor;
	
	ListaArquivo listagem = new ListaArquivo();

	public JList listaArquivosUsuario, listaArquivosServidor;

	JScrollPane scrollListaArquivosUsuario, scrollListaArquivosServidor;

	JButton refreshUsuario, apagarUsuario, infoUsuario, renameUsuario,extButtonUsuario;

	JButton refreshServidor, apagarServidor, infoServidor, renameServidor,extButtonServidor;
	
	JButton editServidor,editCliente;

	JButton enviaServidor;

	JButton enviaCliente;

	public JComboBox listExtCliente,listExtServidor;
	
	public JLabel labelPathUsuario, labelPathServidor, sizeFileUsuario,
			sizeFileServidor, lastModUsuario, lastModServidor,lastAccessServidor,lastAccessUsuario;

	JPanel mainPanelUsuario, mainPanelServidor, mainPanelBotoesServidor,
			mainPanelBotoesUsuario, mainPanelBotoesTransferencia;

	ArrayList<String> arrPastas = new ArrayList<String>();
	public ArrayList<String> arrArquivos = new ArrayList<String>();
	
	public ArrayList<String> arrArquivosServidor = new ArrayList<String>();
	
	public String currentPathUsuario = "/";
	final public String ABSOLUTEPATHUSUARIO = "/";
	public String currentPathServidor = "/";
	public String ABSOLUTEPATHSERVIDOR = "/";

//	public String currentPathUsuario = "/";
//	final public String ABSOLUTEPATHUSUARIO = "/";
//	public String currentPathServidor = "/Users/gbomfim/Downloads";
//	public String ABSOLUTEPATHSERVIDOR = "/Users/gbomfim/Downloads";

	
	
	public GridBagConstraints constraint;
	
	RequestManager requestManager = new RequestManager();

	/**
	 * Logica para podermos ter uma instancia unica da classe
	 * @return
	 */
	public static TelaCliente getInstance() {
		return INSTANCE;
	}

	/**
	 * Construtor da classe de Tela , que carrega e constroi toda a interface.
	 */
	private TelaCliente() {
		try {
			iniciaComponentes();
		} catch (IOException e) {
			e.printStackTrace();
		}
		carregaUI();
		repaint();
		pack();
		setResizable(false);
		setVisible(true);
		setFocusable(true);

	}

	/**
	 * Construo a interface nas posicoes estipuladas pelo metodo
	 */
	public void carregaUI() {

		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		setPreferredSize(new Dimension(1000, 800));
		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraint = new GridBagConstraints();

		setLayout(gridBag);

		constraint.fill = GridBagConstraints.VERTICAL;
		
		constraint.gridx = 0;
		constraint.gridy = 1;
		add(mainPanelBotoesUsuario, constraint);

		constraint.gridx = 1;
		constraint.gridy = 1;
		add(mainPanelUsuario, constraint);

		constraint.gridx = 2;
		constraint.gridy = 1;
		add(mainPanelBotoesTransferencia, constraint);

		constraint.gridx = 3;
		constraint.gridy = 1;
		add(mainPanelServidor, constraint);

		constraint.gridx = 4;
		constraint.gridy = 1;
		add(mainPanelBotoesServidor, constraint);
	}

	
	/**
	 * Metodo que apenas agrupa a chamada de todos do metodos de construcao da interface
	 * @throws IOException
	 */
	public void iniciaComponentes() throws IOException {

		iniciaComponenteBotoesUsuario();

		iniciaComponenteUsuario();

		iniciaComponenteBotoesTransferencia();

		iniciaComponenteServidor();

		iniciaComponenteBotoesServidor();

	}

	public void iniciaComponenteBotoesTransferencia() {
		mainPanelBotoesTransferencia = new JPanel();

		Icon leftIcon = new ImageIcon("images/leftleftleft24.png");
		Icon rightIcon = new ImageIcon("images/rightrightright24.png");

		mainPanelBotoesTransferencia.setPreferredSize(new Dimension(100, 600));

		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraint = new GridBagConstraints();

		mainPanelBotoesTransferencia.setLayout(gridBag);

		constraint.fill = GridBagConstraints.VERTICAL;

		// envia servidor
		enviaServidor = new JButton(rightIcon);
		enviaServidor.setText("Enviar");
		enviaServidor.setPreferredSize(new Dimension(100, 50));
		enviaServidor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!listaArquivosUsuario.isSelectionEmpty() 
						&& (!listaArquivosUsuario.getSelectedValue().toString().equals("...")) 
						&& (!listaArquivosUsuario.getSelectedValue().toString().equals("---"))
						&& (listaArquivosUsuario.getSelectedIndex() > arrPastas.size()+1))
				{
					System.out.println("Enviando arquivo cliente->servidor");
					String serverpath = currentPathServidor+"/"+listaArquivosUsuario.getSelectedValue();
					String clientpath = currentPathUsuario+"/"+listaArquivosUsuario.getSelectedValue();
					
					System.out.println(serverpath);
					System.out.println(clientpath);
					
					requestManager.sendFile(clientpath, serverpath);
					requestManager.updateList();
					
					try {
						montaLista(currentPathUsuario);
					} catch (IOException e1) {
						System.out.println("erro ao dar refresh!");
						e1.printStackTrace();
				}
				}
			}
		});

		constraint.gridx = 0;
		constraint.gridy = 0;

		mainPanelBotoesTransferencia.add(enviaServidor, constraint);

		// envia cliente
		enviaCliente = new JButton(leftIcon);
		enviaCliente.setText("Enviar");
		enviaCliente.setPreferredSize(new Dimension(100, 50));
		enviaCliente.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!listaArquivosServidor.isSelectionEmpty() 
						&& (!listaArquivosServidor.getSelectedValue().toString().equals("...")) 
						&& (!listaArquivosServidor.getSelectedValue().toString().equals("---"))
						&& (listaArquivosServidor.getSelectedIndex() > qtPastasServidor+1))
				{
					System.out.println("Enviando arquivo servidor->cliente");
					String serverpath = currentPathServidor+"/"+listaArquivosServidor.getSelectedValue();
					String clientpath = currentPathUsuario+"/"+listaArquivosServidor.getSelectedValue();
					
					System.out.println(serverpath);
					System.out.println(clientpath);
					
					requestManager.getFile(serverpath, clientpath);
					
					try {
						montaLista(currentPathUsuario);
					} catch (IOException e1) {
						System.out.println("erro ao dar refresh!");
						e1.printStackTrace();
					}
				}
			}
		});

		constraint.gridx = 0;
		constraint.gridy = 1;

		mainPanelBotoesTransferencia.add(enviaCliente, constraint);
	}

	public void iniciaComponenteBotoesServidor() {

		mainPanelBotoesServidor = new JPanel();

		Icon refreshIcon = new ImageIcon("images/refresh24.png");
		Icon renameIcon = new ImageIcon("images/tag.png");
		Icon infoIcon = new ImageIcon("images/info.png");
		Icon deleteIcon = new ImageIcon("images/error.png");

		mainPanelBotoesServidor.setPreferredSize(new Dimension(100, 600));

		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraint = new GridBagConstraints();

		mainPanelBotoesServidor.setLayout(gridBag);

		constraint.fill = GridBagConstraints.VERTICAL;
		
		//JComboBox + butao de Upload por extensao
		JPanel panelExt2 = new JPanel();
	
		montaListaExtServidor();
		panelExt2.setLayout(gridBag);
		constraint.gridx = 0;
		constraint.gridy = 1;
		panelExt2.add(listExtServidor,constraint);
		
		Icon fileIcon = new ImageIcon("images/file.png");
		
		extButtonServidor = new JButton(fileIcon);
		extButtonServidor.setText("Download Extensao");
		extButtonServidor.setPreferredSize(new Dimension(100, 50));
		extButtonServidor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					Object extension = listExtServidor.getSelectedItem();
					requestManager.getManyFiles(extension.toString());
			}
		});
		
		constraint.gridx = 0;
		constraint.gridy = 0;
		panelExt2.add(extButtonServidor,constraint);
		
		
		constraint.gridx = 0;
		constraint.gridy = 1;
		mainPanelBotoesServidor.add(panelExt2, constraint);
		//

		// refresh servidor
		refreshServidor = new JButton(refreshIcon);
		refreshServidor.setText("Atualizar");
		refreshServidor.setPreferredSize(new Dimension(100, 50));
		refreshServidor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				requestManager.updateList();
			}
		});

		constraint.gridx = 0;
		constraint.gridy = 2;

		mainPanelBotoesServidor.add(refreshServidor, constraint);

		// Rename icon
		renameServidor = new JButton(renameIcon);
		renameServidor.setText("Renomear");
		renameServidor.setPreferredSize(new Dimension(100, 50));
		renameServidor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
						Object name = listaArquivosServidor.getSelectedValue();
						if (listaArquivosServidor.getSelectedIndex() > qtPastasServidor + 1) {
							String newName = JOptionPane
									.showInputDialog(
											null,
											"Insira o novo nome do arquivo: (inclua a extensao) ",
											"Renomear", 1);
							if (newName != null && !newName.equals(""))
								requestManager.renameFile(name.toString(), newName);
						}
					}
		});

		constraint.gridx = 0;
		constraint.gridy = 3;

		mainPanelBotoesServidor.add(renameServidor, constraint);

		// Delete icon
		apagarServidor = new JButton(deleteIcon);
		apagarServidor.setText("Apagar");
		apagarServidor.setPreferredSize(new Dimension(100, 50));
		apagarServidor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!listaArquivosServidor.isSelectionEmpty() && (!listaArquivosServidor.getSelectedValue().toString().equals("...")) && (!listaArquivosServidor.getSelectedValue().toString().equals("---")))
				{
					String serverpath = currentPathServidor+"/"+listaArquivosServidor.getSelectedValue();
					System.out.println("Excluindo arquivo do servidor: "+serverpath);
					
					requestManager.removeFile(serverpath);
					
					
					requestManager.updateList();
					
					try {
						montaLista(currentPathUsuario);
					} catch (IOException e1) {
						System.out.println("erro ao dar refresh!");
						e1.printStackTrace();
					}
				}
			}
		});

		constraint.gridx = 0;
		constraint.gridy = 4;

		mainPanelBotoesServidor.add(apagarServidor, constraint);

		// info icon
		infoServidor = new JButton(infoIcon);
		infoServidor.setText("Info");
		infoServidor.setPreferredSize(new Dimension(100, 50));
		infoServidor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if((!listaArquivosServidor.isSelectionEmpty()) && (!listaArquivosServidor.getSelectedValue().toString().equals("...")) && (!listaArquivosServidor.getSelectedValue().toString().equals("---")))
					requestManager.getInfo(listaArquivosServidor.getSelectedValue().toString());
			}
		});

		constraint.gridx = 0;
		constraint.gridy = 5;

		mainPanelBotoesServidor.add(infoServidor, constraint);
		
		// editar icon
		editServidor= new JButton();
		editServidor.setText("Editar");
		editServidor.setPreferredSize(new Dimension(100, 50));
		editServidor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainPanelBotoesUsuario.setVisible(false);
				mainPanelUsuario.setVisible(false);
				mainPanelBotoesTransferencia.setVisible(false);
				mainPanelServidor.setVisible(false);
				mainPanelBotoesServidor.setVisible(false);
				
				String serverpath = currentPathServidor+"/"+listaArquivosServidor.getSelectedValue();
        		String tmpPath = currentPathUsuario+"/"+listaArquivosServidor.getSelectedValue()+".tmp";
        		
        		try {
        			
        			Socket socket = new Socket(FtpClient.client.getIpServer(), FtpClient.client.getPortServer());
					requestManager.editRequestFile(socket, serverpath, tmpPath);
					System.out.println("Request do arquivo");
					//abrindo arquivo
        			File file = new File(tmpPath);
            		Desktop desktop = Desktop.getDesktop();
        			desktop.open(file);
        		
					JOptionPane pane = new JOptionPane("Ja acabou a edicao?.");
			        Object[] options = new String[] { "Acabei a edicao!", "Cancelar Edicao!" };
			        pane.setOptions(options);
			        JDialog dialog = pane.createDialog(new JFrame(), "Edicao");
			        dialog.show(true);
			        Object obj = pane.getValue(); 
			        int result = -1;
			        if (options[0].equals(obj)) {
			        	requestManager.editSendFile(socket, serverpath, tmpPath);
			        	System.out.println("saiiiiindo");
			        }
			        else  { 
			        	System.out.println("Cancelei"); 
			        }
			        mainPanelBotoesUsuario.setVisible(true);
					mainPanelUsuario.setVisible(true);
					mainPanelBotoesTransferencia.setVisible(true);
					mainPanelServidor.setVisible(true);
					mainPanelBotoesServidor.setVisible(true);
        		} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});

		constraint.gridx = 0;
		constraint.gridy = 0;

		mainPanelBotoesServidor.add(editServidor, constraint);

	}

	public void iniciaComponenteBotoesUsuario() {

		mainPanelBotoesUsuario = new JPanel();

		Icon refreshIcon = new ImageIcon("images/refresh24.png");
		Icon renameIcon = new ImageIcon("images/tag.png");
		Icon infoIcon = new ImageIcon("images/info.png");
		Icon deleteIcon = new ImageIcon("images/error.png");

		mainPanelBotoesUsuario.setPreferredSize(new Dimension(100, 600));

		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraint = new GridBagConstraints();

		mainPanelBotoesUsuario.setLayout(gridBag);

		constraint.fill = GridBagConstraints.VERTICAL;

		//JComboBox + butao de Upload por extensao
		JPanel panelExt = new JPanel();
	
		montaListaExtCliente();
		panelExt.setLayout(gridBag);
		constraint.gridx = 0;
		constraint.gridy = 1;
		panelExt.add(listExtCliente,constraint);
		
		Icon fileIcon = new ImageIcon("images/file.png");
		
		extButtonUsuario = new JButton(fileIcon);
		extButtonUsuario.setText("upload Extensao");
		extButtonUsuario.setPreferredSize(new Dimension(100, 50));
		extButtonUsuario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					Object extension = listExtCliente.getSelectedItem();
					requestManager.sendManyFiles(extension.toString());
			}
		});
		
		constraint.gridx = 0;
		constraint.gridy = 0;
		panelExt.add(extButtonUsuario,constraint);
		
		
		
		constraint.gridx = 0;
		constraint.gridy = 1;
		mainPanelBotoesUsuario.add(panelExt, constraint);
		//
		
		// refresh servidor
		refreshUsuario = new JButton(refreshIcon);
		refreshUsuario.setText("Atualizar");
		refreshUsuario.setPreferredSize(new Dimension(100, 50));
		refreshUsuario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					montaLista(currentPathUsuario);
				} catch (IOException e1) {
					System.out.println("erro ao dar refresh!");
					e1.printStackTrace();
				}
			}
		});

		constraint.gridx = 0;
		constraint.gridy = 2;

		mainPanelBotoesUsuario.add(refreshUsuario, constraint);

		// Rename icon
		renameUsuario = new JButton(renameIcon);
		renameUsuario.setText("Renomear");
		renameUsuario.setPreferredSize(new Dimension(100, 50));
		renameUsuario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!listaArquivosUsuario.isSelectionEmpty() && (!listaArquivosUsuario.getSelectedValue().toString().equals("...")) && (!listaArquivosUsuario.getSelectedValue().toString().equals("---")))
				{
					if (listaArquivosUsuario.getSelectedIndex() > qtPastasUser + 1) {
					String newName = JOptionPane
							.showInputDialog(
									null,
									"Insira o novo nome do arquivo: (inclua a extensao) ",
									"Renomear", 1);
					if (newName != null && !newName.equals(""))
						try {
							renameArquivoCliente(newName);
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
				}
			}
		});

		constraint.gridx = 0;
		constraint.gridy = 3;

		mainPanelBotoesUsuario.add(renameUsuario, constraint);

		// Delete icon
		apagarUsuario = new JButton(deleteIcon);
		apagarUsuario.setText("Apagar");
		apagarUsuario.setPreferredSize(new Dimension(100, 50));
		apagarUsuario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!listaArquivosUsuario.isSelectionEmpty() && (!listaArquivosUsuario.getSelectedValue().toString().equals("...")) && (!listaArquivosUsuario.getSelectedValue().toString().equals("---")))
				{
					try {
						deleteArquivoCliente();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});

		constraint.gridx = 0;
		constraint.gridy = 4;

		mainPanelBotoesUsuario.add(apagarUsuario, constraint);

		// info icon
		infoUsuario = new JButton(infoIcon);
		infoUsuario.setText("Info");
		infoUsuario.setPreferredSize(new Dimension(100, 50));
		infoUsuario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!listaArquivosUsuario.isSelectionEmpty() && (!listaArquivosUsuario.getSelectedValue().toString().equals("...")) && (!listaArquivosUsuario.getSelectedValue().toString().equals("---")))
				{
					showInfoCliente();
				}
			}
		});

		constraint.gridx = 0;
		constraint.gridy = 5;

		mainPanelBotoesUsuario.add(infoUsuario, constraint);		
	}

	public void iniciaComponenteUsuario() throws IOException {
		
		// Crio a lista de itens do usuario
		mainPanelUsuario = new JPanel();
		listaArquivosUsuario = new JList();

		montaJListUsuario();

		mainPanelUsuario.setPreferredSize(new Dimension(300, 600));
		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraint = new GridBagConstraints();

		mainPanelUsuario.setLayout(gridBag);

		constraint.fill = GridBagConstraints.VERTICAL;
		
		JLabel labelCliente = new JLabel("CLIENTE");
		
		constraint.gridx = 0;
		constraint.gridy = 1;

		mainPanelUsuario.add(labelCliente, constraint);
		
		constraint.gridx = 0;
		constraint.gridy = 2;
		mainPanelUsuario.add(scrollListaArquivosUsuario, constraint);

		JPanel panelInfo = new JPanel();

		panelInfo.setPreferredSize(new Dimension(300, 100));
		panelInfo.setLayout(gridBag);

		constraint.anchor = GridBagConstraints.WEST;

		constraint.gridx = 0;
		constraint.gridy = 3;
		mainPanelUsuario.add(panelInfo, constraint);

		JLabel labelSize = new JLabel("Tamanho :", JLabel.LEFT);
		constraint.gridx = 0;
		constraint.gridy = 0;
		panelInfo.add(labelSize, constraint);

		JLabel labelMod = new JLabel("Modificado :", JLabel.LEFT);
		constraint.gridx = 0;
		constraint.gridy = 1;
		panelInfo.add(labelMod, constraint);
		
		JLabel labelAccess = new JLabel("Ultimo Acesso:", JLabel.LEFT);
		constraint.gridx = 0;
		constraint.gridy = 2;
		panelInfo.add(labelAccess, constraint);

		sizeFileUsuario = new JLabel();
		constraint.gridx = 1;
		constraint.gridy = 0;
		panelInfo.add(sizeFileUsuario, constraint);

		lastModUsuario = new JLabel();
		constraint.gridx = 1;
		constraint.gridy = 1;
		panelInfo.add(lastModUsuario, constraint);
		
		lastAccessUsuario = new JLabel();
		constraint.gridx = 1;
		constraint.gridy = 2;
		panelInfo.add(lastAccessUsuario, constraint);

	}

	public void iniciaComponenteServidor() throws IOException {

		mainPanelServidor = new JPanel();
		listaArquivosServidor = new JList();

		montaJListServidor();

		mainPanelServidor.setPreferredSize(new Dimension(300, 600));

		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraint = new GridBagConstraints();

		mainPanelServidor.setLayout(gridBag);

		constraint.fill = GridBagConstraints.VERTICAL;
		
		JLabel labelServidor = new JLabel("SERVIDOR");
		
		constraint.gridx = 0;
		constraint.gridy = 1;

		mainPanelServidor.add(labelServidor, constraint);
		
		constraint.gridx = 0;
		constraint.gridy = 2;

		mainPanelServidor.add(scrollListaArquivosServidor, constraint);
		
		JPanel panelInfoServidor = new JPanel();

		panelInfoServidor.setPreferredSize(new Dimension(300, 100));
		panelInfoServidor.setLayout(gridBag);

		constraint.anchor = GridBagConstraints.WEST;

		constraint.gridx = 0;
		constraint.gridy = 3;
		mainPanelServidor.add(panelInfoServidor, constraint);

		JLabel labelSizeServidor = new JLabel("Tamanho :", JLabel.LEFT);
		constraint.gridx = 0;
		constraint.gridy = 0;
		panelInfoServidor.add(labelSizeServidor, constraint);

		JLabel labelModServidor = new JLabel("Modificado :", JLabel.LEFT);
		constraint.gridx = 0;
		constraint.gridy = 1;
		panelInfoServidor.add(labelModServidor, constraint);
		
		JLabel labelAccess = new JLabel("Ultimo Acesso:", JLabel.LEFT);
		constraint.gridx = 0;
		constraint.gridy = 2;
		panelInfoServidor.add(labelAccess, constraint);

		sizeFileServidor = new JLabel();
		constraint.gridx = 1;
		constraint.gridy = 0;
		panelInfoServidor.add(sizeFileServidor, constraint);

		lastModServidor = new JLabel();
		constraint.gridx = 1;
		constraint.gridy = 1;
		panelInfoServidor.add(lastModServidor, constraint);
		
		lastAccessServidor = new JLabel();
		constraint.gridx = 1;
		constraint.gridy = 2;
		panelInfoServidor.add(lastAccessServidor, constraint);

		
	}

	public void montaJListServidor() throws IOException {

		MouseAdapter mouseListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					int index = listaArquivosServidor.locationToIndex(e.getPoint());
					try {
						System.out.println("TENTO");
						trataSelecaoListaServidor(index);
					} catch (IOException e1) {
						System.out.println("erro ao atualizar lista do servidor");
					}
				}
				listaArquivosServidor.repaint();
			}
		};

		listaArquivosServidor.addMouseListener(mouseListener);

		listaArquivosServidor.setLayoutOrientation(JList.VERTICAL);
		listaArquivosServidor.setVisibleRowCount(-1);
		listaArquivosServidor.setBorder(BorderFactory.createLoweredBevelBorder());
		listaArquivosServidor.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		scrollListaArquivosServidor = new JScrollPane(listaArquivosServidor);
		scrollListaArquivosServidor.setPreferredSize(new Dimension(280, 400));

	}

	public void montaJListUsuario() throws IOException {
		montaLista(currentPathUsuario);

		MouseAdapter mouseListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					int index = listaArquivosUsuario.locationToIndex(e
							.getPoint());
					try {
						trataSelecaoListaUsuario(index);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
					listaArquivosUsuario.repaint();
				}
			}
		};

		listaArquivosUsuario.addMouseListener(mouseListener);

		listaArquivosUsuario.setLayoutOrientation(JList.VERTICAL);
		listaArquivosUsuario.setVisibleRowCount(-1);
		listaArquivosUsuario
				.setBorder(BorderFactory.createLoweredBevelBorder());
		listaArquivosUsuario
				.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		scrollListaArquivosUsuario = new JScrollPane(listaArquivosUsuario);
		scrollListaArquivosUsuario.setPreferredSize(new Dimension(280, 400));

	}

	public void trataSelecaoListaUsuario(int index) throws IOException {

		Object objName = listaArquivosUsuario.getSelectedValue();

		int tamListaPastas = arrPastas.size();

		if (objName.toString().equals("---")) {
			return;
		}

		else if (objName.toString().equals("...")) {
			int indexSubString = currentPathUsuario.lastIndexOf("/");
			System.out.println(indexSubString);
			currentPathUsuario = currentPathUsuario
					.substring(0, indexSubString);
			System.out.println(currentPathUsuario);
			montaLista(currentPathUsuario);
			return;
		} else if (index <= tamListaPastas) {

			currentPathUsuario = currentPathUsuario.concat("/"
					+ objName.toString());
			montaLista(currentPathUsuario);
			return;
		}
		// tratamento para solicitar o arquivo para os servidor.
		else
			return;

	}

	public void trataSelecaoListaServidor(int index) throws IOException
	{
			Object objName = listaArquivosServidor.getSelectedValue();

			if (objName.toString().equals("---")) {
				return;
			}

			else if (objName.toString().equals("...")) {
				int indexSubString = currentPathServidor.lastIndexOf("/");
				
				currentPathServidor = currentPathServidor.substring(0, indexSubString);
				
				requestManager.updateList();
				return;
			} else if (index <= qtPastasServidor) {

				currentPathServidor= currentPathServidor.concat("/"
						+ objName.toString());
				requestManager.updateList();
				return;
			}
			// tratamento para solicitar o arquivo para os servidor.
			else
				return;

	}
	
	public void montaLista(String path) throws IOException {

		arrPastas = listagem.listaPastas(path);
		qtPastasUser = arrPastas.size();
		arrArquivos = listagem.listaArquivos(path);
		qtArqUser = arrArquivos.size();
		
		listaArquivosUsuario.setAutoscrolls(true);
		listaArquivosUsuario.setSize(200, 300);

		ArrayList<String> arrTotal = new ArrayList<String>();

		if (!path.equals(ABSOLUTEPATHUSUARIO)) {
			arrTotal.add("...");
		}
		if (arrPastas.size() != 0) {
			arrTotal.addAll(arrPastas);
			arrTotal.add("---");
		}
		if (arrArquivos.size() != 0) {
			arrTotal.addAll(arrArquivos);
			arrTotal.add("---");
		}
		listaArquivosUsuario.setListData(arrTotal.toArray());
		this.atualizaListaExtCliente();
	}

	public void showInfoCliente() {
		String fileName = listaArquivosUsuario.getSelectedValue().toString();

		String filePath = currentPathUsuario.concat("/".concat(fileName));

		InfoArquivo info = listagem.getInfoArquivo(filePath);

		sizeFileUsuario.setText(String.valueOf(info.getTamanho()) + "  kb");
		lastModUsuario.setText(info.getUltimaMod().toString());

	}

	public void deleteArquivoCliente() throws IOException {
		String fileName = listaArquivosUsuario.getSelectedValue().toString();
		String filePath = currentPathUsuario.concat("/".concat(fileName));

		listagem.deleteArquivo(filePath);
		montaLista(currentPathUsuario);

	}

	public void renameArquivoCliente(String newName) throws IOException {
		String fileName = listaArquivosUsuario.getSelectedValue().toString();
		String filePath = currentPathUsuario.concat("/".concat(fileName));
		String novoNome = currentPathUsuario.concat("/".concat(newName));
		listagem.renameArquivo(filePath, novoNome);
		montaLista(currentPathUsuario);

	}
	
	public void atualizaListaExtCliente()
	{
		ArrayList<String> arrExt = new ArrayList<>();
		try {
			arrExt = listagem.listaExtensoesPorPasta(currentPathUsuario);
		} catch (IOException e) {
			System.out.println("Erro ao listar as extensoes");
			e.printStackTrace();
		}
		
		this.listExtCliente.removeAllItems();
		for (int i = 0; i < arrExt.size(); i++) {
				this.listExtCliente.insertItemAt(arrExt.get(i), i);
		}

	}
	
	public void montaListaExtCliente()
	{
		
		ArrayList<String> arrExt = new ArrayList<String>();
		try {
			arrExt = listagem.listaExtensoesPorPasta(currentPathUsuario);
		} catch (IOException e) {
			System.out.println("Erro ao listar as extensoes");
			e.printStackTrace();
		}
		listExtCliente = new JComboBox();
		
		for (int i = 0; i < arrExt.size(); i++) {
			listExtCliente.insertItemAt(arrExt.get(i), i);
		}
		
	}
	
	public void montaListaExtServidor()
	{
		ArrayList<String> arrExt = new ArrayList<String>();
		arrExt.add("joao");
		arrExt.add("maria");
		arrExt.add("jose");
		listExtServidor = new JComboBox(arrExt.toArray());
	}
	
}
